<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./OL_SDK/include-openlayers-local.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0
        }

        #map {
            width: 100vw;
            height: 100vh;
        }
    </style>
</head>

<body>
    <div id="map">

    </div>
    <script>
        /* 第三方的地图 */
        var gaodeMapLayer = new ol.layer.Tile({
            title: "高德地图-矢量图层",
            source: new ol.source.XYZ({
                url: 'http://wprd0{1-4}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&style=7&x={x}&y={y}&z={z}',
                wrapX: false
            })
        });
        var image = new ol.layer.Tile({
            title: "高德地图-影像图层",
            source: new ol.source.XYZ({
                url: "http://wprd0{1-4}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&style=6&x={x}&y={y}&z={z}",
                wrapX: false
            })
        })
        const map = new ol.Map({
            target: "map",
            layers: [gaodeMapLayer, image],
            view: new ol.View({
                center: [114.30, 30.50],
                zoom: 6,
                projection: "EPSG:4326"
            })
        })
        var isShow = false;
        map.on("click", () => {
            isShow = !isShow;
            var layers = map.getLayers().getArray();
            console.log(layers[1])
            // layers[1].setOpacity(0)
            // if (isShow) {
            //     layers[1].setVisible(false)
            // } else {
            //     layers[1].setVisible(true)
            // }
            if (isShow) {
                layers[1].setOpacity(0)
            } else {
                layers[1].setOpacity(1)
            }
        })
    </script>
</body>

</html>